package com.chromeframe.client;

import com.chromeframe.client.Resources.InstallerCss;
import com.google.gwt.core.client.GWT;
import com.google.gwt.dom.client.StyleInjector;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.VerticalPanel;

/**
 * Chrome frame installer.
 * @author Gal Dolber
 */
public class Installer extends Composite {

	/**
	 * Chrome frame home page.
	 */
	private static final String CHROMEFRAMEHOME = "http://code.google.com/intl/en/chrome/chromeframe/";

	/**
	 * Chrome home.
	 */
	private static final String CHROMEHOME = "http://www.google.com/chrome/";

	/**
	 * Firefox home.
	 */
	private static final String FIREFOXHOME = "http://www.mozilla-europe.org/en/firefox/";

	/**
	 * Safari home.
	 */
	private static final String SAFARIHOME = "http://www.apple.com/es/safari/";

	/**
	 * Chrome install.
	 */
	private static final String CHROMEINSTALL = "http://www.google.com/chromeframe/eula.html";

	/**
	 * Css.
	 */
	private static InstallerCss css = Resources.CSS;
	
	static {
		StyleInjector.injectStylesheet(Resources.CSS.getText());
	}
	
	/**
	 * Images.
	 */
	private Images images = GWT.create(Images.class);

	/**
	 * Horizontal panel.
	 */
	private HorizontalPanel panel = new HorizontalPanel();
	
	/**
	 * Vertical panel.
	 */
	private VerticalPanel vertical = new VerticalPanel();

	/**
	 * Chrome image.
	 */
	private Image chromeImage = images.chrome().createImage();
	
	/**
	 * Safari image.
	 */
	private Image safariImage = images.safari().createImage();
	
	/**
	 * Firefox image.
	 */
	private Image firefoxImage = images.firefox().createImage();
	
	/**
	 * Ie image.
	 */
	private Image ieImage = images.ie().createImage();
	
	/**
	 * Message.
	 */
	private HTML message = new HTML();

	/**
	 * Chrome handler.
	 */
	private ClickHandler chromeHandler = new ClickHandler() {
		@Override
		public void onClick(ClickEvent event) {
			Window.Location.assign(CHROMEHOME);
		}
	};

	/**
	 * Firefox handler.
	 */
	private ClickHandler firefoxHandler = new ClickHandler() {
		@Override
		public void onClick(ClickEvent event) {
			Window.Location.assign(FIREFOXHOME);
		}
	};

	/**
	 * Safari handler.
	 */
	private ClickHandler safariHandler = new ClickHandler() {
		@Override
		public void onClick(ClickEvent event) {
			Window.Location.assign(SAFARIHOME);
		}
	};

	/**
	 * IE handler.
	 */
	private ClickHandler ieHandler = new ClickHandler() {
		@Override
		public void onClick(ClickEvent event) {
			Window.Location.assign(CHROMEINSTALL);
		}
	};
	
	/**
	 * Constructor.
	 * 
	 * @param appName Application name
	 */
	public Installer() {
		
		message.setStyleName(css.message());
		
		message.setHTML("This page needs " +
				"<a href='" + CHROMEFRAMEHOME + "'>" +
				"Google Chrome Frame </a>" +
				" to work correctly on Internet Explorer. " +
				"Or you can use any of the following browsers.");
		
		vertical.add(message);
		vertical.add(panel);
		vertical.add(ieImage);
		vertical.setCellHorizontalAlignment(panel, HasHorizontalAlignment.ALIGN_CENTER);
		vertical.setCellHorizontalAlignment(ieImage, HasHorizontalAlignment.ALIGN_CENTER);
		
		chromeImage.addClickHandler(chromeHandler);
		firefoxImage.addClickHandler(firefoxHandler);
		safariImage.addClickHandler(safariHandler);
		ieImage.addClickHandler(ieHandler);
		
		panel.add(chromeImage);
		panel.add(safariImage);
		panel.add(firefoxImage);
		
		initWidget(vertical);
		
		setStyleName(css.installer());
	}
	
}
